package com.molyfun.parents.fragment.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.Const;

public class CommonDeleteDialogFragment extends DialogFragment implements View.OnClickListener{

    private static CommonDeleteDialogFragment newInstance( String content) {
        CommonDeleteDialogFragment mStudentMenuDialogFragment = new CommonDeleteDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Const.FLAG_CONTENT, content);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_common_delete, container, false);
        String content = (String)getArguments().get(Const.FLAG_CONTENT);


        ((TextView)view.findViewById(R.id.content)).setText(content);
        view.findViewById(R.id.cancel).setOnClickListener(this);
        view.findViewById(R.id.commit).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel:
                if(mCommonChooseDialogClickListener != null){
                    mCommonChooseDialogClickListener.onDeleteCancel();
                }
                break;
            case R.id.commit:
                if(mCommonChooseDialogClickListener != null){
                    mCommonChooseDialogClickListener.onDeleteConfirm();
                }
                break;
            default:
                break;
        }
        getDialog().dismiss();
    }

    public interface CommonDeleteDialogClickListener {
        public void onDeleteConfirm();
        public void onDeleteCancel();
    }

    private CommonDeleteDialogClickListener mCommonChooseDialogClickListener;

    public void setCommonChooseDialogClickListener(CommonDeleteDialogClickListener commonChooseDialogClickListener){
        this.mCommonChooseDialogClickListener = commonChooseDialogClickListener;
    }


    public static class CommonDeleteDialogBuilder {

        CommonDeleteDialogFragment mCommonDialogFragment;
        private String mTag;
        private CommonDeleteDialogClickListener mCommonDeleteDialogClickListener;

        public CommonDeleteDialogBuilder(String content){
            mCommonDialogFragment = CommonDeleteDialogFragment.newInstance(content);
            mTag = "common_choose_notice";
        }

        public CommonDeleteDialogBuilder setCommonDeleteDialogClickListener(CommonDeleteDialogClickListener commonDeleteDialogClickListener){
            mCommonDialogFragment.setCommonChooseDialogClickListener(commonDeleteDialogClickListener);
            return this;
        }


        public CommonDeleteDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
//            mCommonDialogFragment.show(fragmentTransaction, mTag);
            fragmentTransaction.add(mCommonDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
